#include "SyncObjects.h"

mini_engine::SyncObjects* mini_engine::SyncObjects::m_syncObjects=nullptr;
VkSemaphore mini_engine::SyncObjects::imageAvailableSemaphore;
VkSemaphore mini_engine::SyncObjects::renderFinishedSemaphore;
VkFence mini_engine::SyncObjects::inFlightFence;

mini_engine::SyncObjects::SyncObjects()
{
}

void mini_engine::SyncObjects::createSyncObjects()
{
    cout << "createSyncObjects" << endl;

    VkSemaphoreCreateInfo semaphoreInfo{};
    semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;

    VkFenceCreateInfo fenceInfo{};
    fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
    fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;

    if (vkCreateSemaphore(LogicalDevice::logicalDevice, &semaphoreInfo, nullptr, &imageAvailableSemaphore) != VK_SUCCESS ||
        vkCreateSemaphore(LogicalDevice::logicalDevice, &semaphoreInfo, nullptr, &renderFinishedSemaphore) != VK_SUCCESS ||
        vkCreateFence(LogicalDevice::logicalDevice, &fenceInfo, nullptr, &inFlightFence) != VK_SUCCESS)
    {
        throw std::runtime_error("failed to create synchronization objects for a frame!");
    }
}

mini_engine::SyncObjects::~SyncObjects()
{
}

mini_engine::SyncObjects *mini_engine::SyncObjects::getSyncObjects()
{
    if (m_syncObjects == nullptr)
    {
        m_syncObjects = new SyncObjects();
        SyncObjectsDestruction syncObjectsDestruction;
    }
    return m_syncObjects;
}

mini_engine::SyncObjects::SyncObjectsDestruction::~SyncObjectsDestruction()
{
    if (m_syncObjects != nullptr)
    {
        delete m_syncObjects;
        m_syncObjects = nullptr;
    }
}